﻿// CPasswd.cpp: 实现文件
//

#include "stdafx.h"
#include "UsbPhTest.h"
#include "CPasswd.h"
#include "afxdialogex.h"

TCHAR* GetAppPath(TCHAR* PathBuf, int BufSize)
{
	ZeroMemory(PathBuf, BufSize);
	GetModuleFileName(AfxGetApp()->m_hInstance, PathBuf, BufSize - 1);
	PathRemoveFileSpec(PathBuf);
	_tcscat(PathBuf, _T("\\"));
	return PathBuf;
}


// CPasswd 对话框

IMPLEMENT_DYNAMIC(CPasswd, CDialog)


CPasswd::CPasswd(CWnd* pParent /*=nullptr*/)
	: CDialog(IDD_CPasswd, pParent)
	, m_passwd(_T(""))
{
	int m_videotime = 0;
	char szTmp[128] = { 0 };
	char m_szAppPath[256] = { 0 };
	GetAppPath(m_szAppPath, sizeof(m_szAppPath));
	strcat(m_szAppPath, "config.ini");
	GetPrivateProfileString("GLOBAL", "Admin", "7276", szTmp, sizeof(szTmp) - 1, m_szAppPath);//读出string
	m_admin = szTmp;
}

CPasswd::~CPasswd()
{
}

void CPasswd::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT1, m_passwd);
	DDX_Control(pDX, IDC_STR_INFO, m_txtInfo);
}


BEGIN_MESSAGE_MAP(CPasswd, CDialog)
	ON_WM_CLOSE()
	ON_WM_DESTROY()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDOK, &CPasswd::OnBnClickedOk)
	ON_STN_CLICKED(IDC_STR_INFO, &CPasswd::OnStnClickedStrInfo)
END_MESSAGE_MAP()


// CPasswd 消息处理程序


BOOL CPasswd::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  在此添加额外的初始化

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CPasswd::OnClose()
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	CDialog::OnClose();
}


void CPasswd::OnDestroy()
{
	CDialog::OnDestroy();

	// TODO: 在此处添加消息处理程序代码
}


void CPasswd::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	CDialog::OnTimer(nIDEvent);
}

int g_trytimes = 0;
void CPasswd::OnOK()
{
	// TODO: 在此添加专用代码和/或调用基类
	this->UpdateData();
	m_txtInfo.SetWindowText("");
	if (m_passwd.IsEmpty() || m_passwd.GetLength() < 2) {
		m_txtInfo.SetWindowText("请输入密码");
		return;
	}
	if (m_passwd != "7276") {
		g_trytimes++;
		Sleep((g_trytimes * g_trytimes) * 200);
		m_txtInfo.SetWindowText("密码有误，请重新输入正确密码");
		return;
	}
	g_trytimes = 0;

	CDialog::OnOK();
}


BOOL CPasswd::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类

	return CDialog::PreTranslateMessage(pMsg);
}


void CPasswd::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	this->OnOK();
}


void CPasswd::OnStnClickedStrInfo()
{
	// TODO: 在此添加控件通知处理程序代码
}
